import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import DengluPage from '@/views/Denglu/DengluPage.vue'
import FengleiPage from '@/views/Fenglei/FengleiPage.vue'
import GowuchePage from '@/views/Gowuche/GowuchePage.vue'
import JiesuanZhifu from '@/views/JiesuanZhifu/JiesuanZhifu.vue'
import Shangpingxiangping from '@/views/Shangpingxiangping/ShangpingxiangpingPage.vue'
import Shoye from '@/views/Shoye/ShoyePage.vue'
import ShoyeJiaozi from '@/views/ShoyeJiaozi/ShoyeJiaozi.vue'
import Sosuo from '@/views/Sosuo/SosuoPage.vue'
import SosuoLiebiao from '@/views/SosuoLiebiao/SosuoLiebiao.vue'
import Wode from '@/views/wode/WodePage.vue'
import WodeDingdan from '@/views/WodeDingdan/WodeDingdan.vue'
import ChuCuo from '@/components/ChuCuo.vue'
import { useMemberStore } from '@/strres/modules/member'
import { Pinia } from 'pinia'
Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  {
    path: '/',
    component: ShoyeJiaozi,
    redirect: '/Shoye',
    children: [
      {
        path: 'FengleiPage',
        name: 'FengleiPage',
        component: FengleiPage
      },
      {
        path: '/Shoye',
        name: 'Shoye',
        component: Shoye
      },
      {
        path: 'GowuchePage',
        name: 'GowuchePage',
        component: GowuchePage
      },
      {
        path: 'Wode',
        name: 'Wode',
        component: Wode
      }
    ]
  },
  {
    path: '/Sosuo',
    name: 'Sosuo',
    component: Sosuo
  },
  {
    path: '/Shangpingxiangping/:id',
    name: 'Shangpingxiangping',
    component: Shangpingxiangping
  },
  {
    path: '/JiesuanZhifu',
    name: 'JiesuanZhifu',
    component: JiesuanZhifu
  },
  {
    path: '/DengluPage',
    name: 'DengluPage',
    component: DengluPage
  },
  {
    path: '/SosuoLiebiao',
    name: 'SosuoLiebiao',
    component: SosuoLiebiao
  },
  {
    path: '/WodeDingdan',
    name: 'WodeDingdan',
    component: WodeDingdan
  },
  {
    path: '*',
    component: ChuCuo
  }
]

const router = new VueRouter({
  routes
})

export function setupRouterGuards (pinia: Pinia) {
  const authUrl = ['/DengluPage', '/JiesuanZhifu']
  router.beforeEach((to, from, next) => {
    const useMemberStores = useMemberStore(pinia)
    const token = useMemberStores.token
    if (!authUrl.includes(to.path)) {
      next()
      return
    }
    if (token) {
      next()
    } else {
      next('/DengluPage')
    }
  })
}

export default router
